package com.easylive.web.controller;

import com.easylive.web.annotation.GlobalInterceptor;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.*;
import com.easylive.entity.po.*;
import com.easylive.entity.query.*;
import com.easylive.entity.request.uhome.WebUHomeSaveVideoSeriesReqVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.UserVideoSeriesDetailVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yudaxian
 * @DateTime: 2024/12/2 16:26
 **/
@RestController
@RequestMapping("/uhome/series")
public class UHomeSeriesController extends ABaseController {

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private UserVideoSeriesService userVideoSeriesService;

    @Resource
    private UserVideoSeriesVideoService userVideoSeriesVideoService;

    /**
     * 加载用户的视频合集列表
     * @param userId
     * @return
     */
    @PostMapping("/loadVideoSeries")
    public ResponseVO loadVideoSeries(@Validated @NotBlank String userId) {

        List<UserVideoSeries> list = userVideoSeriesService.getUserAllSeries(userId);

        return getSuccessResponseVO(list);
    }

    /**
     * 保存视频合集，需要往合集表插入数据，然后往合集对应的视频表中插入视频数据
     * @param reqVO
     * @return
     */
    @PostMapping("/saveVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveVideoSeries(@Validated WebUHomeSaveVideoSeriesReqVO reqVO) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserVideoSeries userVideoSeries = new UserVideoSeries();
        userVideoSeries.setSeriesId(reqVO.getSeriesId());
        userVideoSeries.setSeriesName(reqVO.getSeriesName());
        userVideoSeries.setSeriesDescription(reqVO.getSeriesDescription());
        userVideoSeries.setUserId(tokenUserInfoDto.getUserId());

        userVideoSeriesService.saveUserVideoSeries(userVideoSeries, reqVO.getVideoIds());

        return getSuccessResponseVO(null);
    }

    /**
     * 加载视频列表，用于导入到合集中，如果有 seriesId，要过滤掉当前合集中的视频
     * @param seriesId
     * @return
     */
    @PostMapping("/loadAllVideo")
    public ResponseVO loadAllVideo(@Validated Integer seriesId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();

        VideoInfoQuery query = new VideoInfoQuery();
        query.setUserId(userId);
        query.setOrderBy("create_time desc");

        if (seriesId != null) {
            // 过滤掉当前合集中的视频
            UserVideoSeriesVideoQuery userVideoSeriesVideoQuery = new UserVideoSeriesVideoQuery();
            userVideoSeriesVideoQuery.setUserId(userId);
            userVideoSeriesVideoQuery.setSeriesId(seriesId);
            List<UserVideoSeriesVideo> videoSeriesVideos = userVideoSeriesVideoService.findListByParam(userVideoSeriesVideoQuery);
            List<String> filterVideoIds = videoSeriesVideos.stream().map(UserVideoSeriesVideo::getVideoId).collect(Collectors.toList());
            if (!filterVideoIds.isEmpty()) {
                query.setFilterVideoIds(filterVideoIds);
            }
        }
        List<VideoInfo> list = videoInfoService.findListByParam(query);
        return getSuccessResponseVO(list);
    }

    /**
     * 获取系列中的具体视频信息
     */
    @PostMapping("/getVideoSeriesDetail")
    public ResponseVO getVideoSeriesDetail(@Validated @NotNull Integer seriesId) {

        UserVideoSeries videoSeries = userVideoSeriesService.getUserVideoSeriesBySeriesId(seriesId);
        if (videoSeries == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }

        UserVideoSeriesVideoQuery userVideoSeriesVideoQuery = new UserVideoSeriesVideoQuery();
        userVideoSeriesVideoQuery.setSeriesId(seriesId);
        userVideoSeriesVideoQuery.setOrderBy("sort asc");
        userVideoSeriesVideoQuery.setQueryVideoInfo(true);
        List<UserVideoSeriesVideo> videoSeriesVideos = userVideoSeriesVideoService.findListByParam(userVideoSeriesVideoQuery);

        UserVideoSeriesDetailVO vo = new UserVideoSeriesDetailVO();
        vo.setVideoSeries(videoSeries);
        vo.setSeriesVideoList(videoSeriesVideos);

        return getSuccessResponseVO(vo);
    }

    /**
     * 保存视频合集信息
     */
    @PostMapping("/saveSeriesVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveSeriesVideo(@Validated @NotBlank String videoIds, @NotNull Integer seriesId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.saveSeriesVideo(tokenUserInfoDto.getUserId(), seriesId, videoIds);

        return getSuccessResponseVO(null);
    }

    /**
     * 删除集合里的视频
     */
    @PostMapping("/delSeriesVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delSeriesVideo(@Validated @NotNull Integer seriesId, @NotBlank String videoId) {

        UserVideoSeries videoSeriesBySeriesId = userVideoSeriesService.getUserVideoSeriesBySeriesId(seriesId);
        if (videoSeriesBySeriesId == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        UserVideoSeriesVideo videoSeries = userVideoSeriesVideoService.getUserVideoSeriesVideoBySeriesIdAndVideoId(seriesId, videoId);
        if (videoSeries == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        // 另一种方法是删除时带上userId，通过token中取出，这样可以避免别人搞破坏

        userVideoSeriesVideoService.deleteUserVideoSeriesVideoBySeriesIdAndVideoId(seriesId, videoId);

        return getSuccessResponseVO(null);
    }

    /**
     * 删除合集
     */
    @PostMapping("/delVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delVideoSeries(@Validated @NotNull Integer seriesId) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();

        userVideoSeriesService.deleteVideoSeries(userId, seriesId);

        return getSuccessResponseVO(null);
    }

    /**
     * 更改视频集合的顺序
     * @param seriesIds
     * @return
     */
    @PostMapping("/changeVideoSeriesSort")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO changeVideoSeriesSort(@Validated @NotBlank String seriesIds) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.changeVideoSeriesSort(tokenUserInfoDto.getUserId(), seriesIds);

        return getSuccessResponseVO(null);
    }

    /**
     * 查集合和对应的视频
     * @return
     */
    @PostMapping("/loadVideoSeriesWithVideo")
    public ResponseVO loadVideoSeriesWithVideo(@Validated @NotBlank String userId) {
        UserVideoSeriesQuery userVideoSeriesQuery = new UserVideoSeriesQuery();
        userVideoSeriesQuery.setUserId(userId);
        userVideoSeriesQuery.setOrderBy("sort asc");
        List<UserVideoSeries> userVideoSeriesList = userVideoSeriesService.findVideoSeriesWithVideo(userVideoSeriesQuery);
        return getSuccessResponseVO(userVideoSeriesList);
    }
}
